import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

// 路由配置
const routes = [

  // 用户登录页面
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  // 管理员登录页面
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/AdminLogin.vue'),
    meta: { requiresAuth: false }
  },
  
  // 用户端路由
  {
    path: '/user',
    name: 'UserLayout',
    component: () => import('@/views/user/Layout.vue'),
    meta: { requiresAuth: true, role: 'user' },
    children: [
      {
        path: 'home',
        name: 'UserHome',
        component: () => import('@/views/user/Home.vue')
      },
      {
        path: 'venues',
        name: 'Venues',
        component: () => import('@/views/user/Venues.vue'),
        meta: { requiresAuth: true, role: 'user' }
      },
      {
        path: 'coaches',
        name: 'Coaches',
        component: () => import('@/views/user/Coaches.vue'),
        meta: { requiresAuth: true, role: 'user' }
      },

      {
        path: 'activities',
        name: 'UserActivities',
        component: () => import('@/views/user/Activities.vue')
      },
      {
        path: 'activity/:id',
        name: 'UserActivityDetail',
        component: () => import('@/views/user/ActivityDetail.vue')
      },
      {
        path: 'bookings',
        name: 'UserBookings',
        component: () => import('@/views/user/Bookings.vue')
      },
      {
        path: 'booking/create',
        name: 'UserBookingCreate',
        component: () => import('@/views/user/BookingCreate.vue')
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('@/views/user/Profile.vue')
      }
    ]
  },
  
  // 管理员端路由
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('@/views/admin/Layout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'venues',
        name: 'AdminVenues',
        component: () => import('@/views/admin/Venues.vue')
      },
      {
        path: 'coaches',
        name: 'AdminCoaches',
        component: () => import('@/views/admin/Coaches.vue')
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/Dashboard.vue')
      },
      {
        path: 'activities',
        name: 'AdminActivities',
        component: () => import('@/views/admin/Activities.vue')
      },
      {
        path: 'activity/create',
        name: 'AdminActivityCreate',
        component: () => import('@/views/admin/ActivityCreate.vue')
      },
      {
        path: 'activity/edit/:id',
        name: 'AdminActivityEdit',
        component: () => import('@/views/admin/ActivityEdit.vue')
      },
      {
        path: 'activity/:id',
        name: 'AdminActivityDetail',
        component: () => import('@/views/admin/ActivityDetail.vue')
      },
      {
        path: 'bookings',
        name: 'AdminBookings',
        component: () => import('@/views/admin/Bookings.vue')
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('@/views/admin/Users.vue')
      }
    ]
  },
  
  // 默认重定向
  {
    path: '/',
    redirect: '/login'
  },
  
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  
  console.log('路由守卫检查:', {
    to: to.path,
    from: from.path,
    token: token ? '存在' : '不存在',
    userInfo: userInfo,
    requiresAuth: to.meta.requiresAuth,
    requiredRole: to.meta.role
  })
  
  // 不需要认证的页面
  if (!to.meta.requiresAuth) {
    console.log('页面不需要认证，直接通过')
    next()
    return
  }
  
  // 检查是否已登录
  if (!token) {
    console.log('Token 不存在，重定向到登录页')
    ElMessage.warning('请先登录')
    next('/login')
    return
  }
  
  // 检查角色权限
  if (to.meta.role && to.meta.role !== userInfo.role) {
    // 如果没有角色信息，默认设置为用户角色
    if (!userInfo.role) {
      console.log('用户信息中没有角色，设置为用户角色')
      userInfo.role = 'user'
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      next()
      return
    }
    
    console.log('权限检查失败:', {
      requiredRole: to.meta.role,
      userRole: userInfo.role,
      userInfo: userInfo
    })
    
    ElMessage.error('权限不足，您没有访问该页面的权限')
    next('/user/home') // 重定向到用户首页而不是登录页
    return
  }
  
  console.log('权限检查通过，允许访问')
  next()
})

export default router 